﻿using CPPEI;
using CPPEI.WebApi;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ENT = HH.Domain.Entities;

namespace HH.WebApi.Articles
{
    [CPPEI.WebApi.GetAction("Article", "GetLatestList", "1.0")]
    public class GetLatestListAction : CPPEI.WebApi.BaseAction<GetLatestListParams, GetLatestListResult>
    {
        protected override CPPEI.WebApi.ActionResult<GetLatestListResult> OnExecute(CPPEI.WebApi.ActionContext context, CPPEI.WebApi.SysParameters sysParams, GetLatestListParams opParams)
        {
            GetLatestListResult actResult = new GetLatestListResult();

            HH.Domain.Services.ArticleService service = new Domain.Services.ArticleService();
            PagedResult<ENT.Article> serResult = service.GetPagedList(opParams.Page * 10, 10);

            if(serResult.IsSuccess)
            {
                for (int index = 0; index < serResult.Data.Count; index++)
                {
                    GetLatestListResult.Item item = new GetLatestListResult.Item();
                    item.Id = serResult.Data[index].Id;
                    item.PublishedTime = serResult.Data[index].PublishedTime.ToString("yyyy-MM-dd");
                    item.Title = serResult.Data[index].Title;

                    actResult.Items.Add(item);
                }
                return new CPPEI.WebApi.ActionResult<GetLatestListResult>(actResult);
            }
            else
            {
                return new ActionResult<GetLatestListResult>(serResult);
            }
        }
    }

    public class GetLatestListParams
    {
        [Default(0)]
        public int Page { get; set; }
    }

    public class GetLatestListResult : PagedResult
    {
        public List<Item> Items = new List<Item>();

        public class Item
        {
            public Guid Id { get; set; }

            public string Title { get; set; }

            public string PublishedTime { get; set; }
        }
    }
}
